
local monthly_var_year  = 1997
local local hpi_futureq = 93

tostring(survey_year), gen(asmb_year)


** create monthly versions of the delinquency variables
foreach varname in delqauto delqcard delqothr delqstud delq2nd {
	forvalues quarter = 61(1)92 {

		local next_quarter = `quarter' + 1
		local monthly_var_year = cond(mod(`quarter',4) == 1,`monthly_var_year' + 1,`monthly_var_year')

		
			* create month variables in ascending order
			forvalues month = 2(-1)0 {
				
				local monthcount = 3*`quarter' - `month'
				local prev_monthcount = `monthcount' - 1
				* track the month it is in a calendar year;
				local month_out_of_year = cond(mod(`monthcount',12) == 0, 12, mod(`monthcount',12))
				
			
				* Part b: open-ended, close-ended lien - convert to monthly
				gen `varname'm`monthcount' = cond(mi(`varname'q`quarter'), 0,`varname'q`quarter')
				label variable `varname'm`monthcount' "`varname'-Month `month_out_of_year', `monthly_var_year'"	

			}
	}
}


foreach var in delqany delq2any {
	gen `var'_grid = ""
	gen `var'_grid_long = ""
}
	
	
* create variables with the survey start and end dates
gen survey_start = cond(asmb_year == "2016",200,cond(asmb_year == "2017", 211,cond(asmb_year == "2018", 222,274)))

gen survey_start_long = survey_start - 24

gen survey_end = cond(asmb_year == "2016",227,cond(asmb_year == "2017", 238,cond(asmb_year == "2018", 249,274)))
	
	
	

* Create a grid to track 60+ day delinquency on any non-mortgage credit product
forvalues mm = 181(1)276 {
	gen delqanym`mm' = "1" if !inlist(delqautom`mm',0,1) | !inlist(delqcardm`mm',0,1) | !inlist(delqothrm`mm',0,1) | !inlist(delqstudm`mm',0,1)
	replace delqanym`mm' = "0" if mi(delqanym`mm')
	
	foreach var in delqany  {
		* Get performance grid up to two years before the survey
		replace `var'_grid = `var'_grid + `var'm`mm' if `mm' >= survey_start & `mm' <= survey_end
		replace `var'_grid = subinstr(`var'_grid, ".", "",.)
	}	
	
}

* Finally, create the indicator variable we want
gen everdelqany = 100*(regexm(delqany_grid,"1"))
